package cn.czh0123.service.impl;

import cn.czh0123.domain.Comment;
import cn.czh0123.domain.User;
import cn.czh0123.enums.WallpaperOperateType;
import cn.czh0123.mapper.CommentMapper;
import cn.czh0123.query.CommentQuery;
import cn.czh0123.redis.service.IWallpaperOperateRedisService;
import cn.czh0123.service.ICommentService;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
* 评论服务接口实现
*/
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper,Comment> implements ICommentService  {

    @Resource
    private IWallpaperOperateRedisService wallpaperOperateRedisService;
    @Resource
    private CommentMapper commentMapper;
    @Override
    public IPage<Comment> queryPage(CommentQuery qo) {
        Page<Comment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return commentMapper.selectCommentPage(page, qo);
    }

    @Override
    public Comment saveComment(Comment comment) {
        User user = (User) StpUtil.getSession().get("user");
        comment.setUserId(user.getId());
        comment.setCommentTime(new Date());
        wallpaperOperateRedisService.operate(comment.getWallpaperId(), WallpaperOperateType.COMMENT);
        commentMapper.insert(comment);
        comment.setUserName(user.getUsername());
        comment.setUserAvatar(user.getAvatar());
        return comment;
    }
}
